package com.vhall.component.service.account.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.account.GuosenStaffMapper;
import com.vhall.component.entity.account.GuosenStaffEntity;
import com.vhall.component.entity.room.vo.GuoSenStaffListRequestVO;
import com.vhall.component.entity.room.vo.GuoSenStaffListResponseVO;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.account.GuosenStaffService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xing.wang
 * @description 针对表【guosen_staff】的数据库操作Service实现
 * @createDate 2023-08-08 16:32:54
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GuosenStaffServiceImpl extends ServiceImpl<GuosenStaffMapper, GuosenStaffEntity>
        implements GuosenStaffService {
    private final GuosenStaffMapper guosenStaffMapper;

    @Override
    public List<GuoSenStaffListResponseVO> getGuoXinStaffList(GuoSenStaffListRequestVO vo) {
        LambdaQueryWrapper<GuosenStaffEntity> queryWrapper = Wrappers.lambdaQuery();
        if (vo.getSameDepartRestric() != null && vo.getSameDepartRestric()) {
            Token token = TokenThreadLocal.getToken();
            String thirdPartyUserId = token.getThirdPartyUserId();
            queryWrapper.eq(GuosenStaffEntity::getOrgcode, token.getOrgCode());
            queryWrapper.ne(GuosenStaffEntity::getNotesid, thirdPartyUserId);
        }
        String notesid = vo.getNotesid();
        String keyword = vo.getKeyword();
        queryWrapper.eq(StrUtil.isNotBlank(notesid), GuosenStaffEntity::getNotesid, notesid)
        .and(StrUtil.isNotBlank(keyword),w->w.eq(GuosenStaffEntity::getNotesid,keyword).or().like(GuosenStaffEntity::getStaffname,keyword))
        .last(" limit 100 ");
        List<GuosenStaffEntity> guosenStaffEntities = guosenStaffMapper.selectList(queryWrapper);
        List<GuoSenStaffListResponseVO> result = new ArrayList<>();
        if (CollUtil.isNotEmpty(guosenStaffEntities)) {
            result = guosenStaffEntities.stream().map(data -> {
                GuoSenStaffListResponseVO temp = new GuoSenStaffListResponseVO();
                temp.setStaffname(data.getStaffname());
                String cyzgzbh = data.getCyzgzbh();
                String zyzg = data.getZyzg();
//                不是 证券投资咨询业务(投资顾问) 则显示为无
                if (StrUtil.isBlank(zyzg) || !StrUtil.contains(zyzg, "证券投资咨询业务") || !StrUtil.contains(zyzg, "投资顾问")) {
                    cyzgzbh = "暂无投顾执业资格编号";
                }
                cyzgzbh = StrUtil.isBlank(cyzgzbh)?"暂无投顾执业资格编号":cyzgzbh;
                temp.setCyzgzbh(cyzgzbh);
                temp.setUserCode(data.getNotesid());
                return temp;
            }).collect(Collectors.toList());
        }
        return result;
    }
}




